package com.jzo2o.foundations.service.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.ServeItemMapper;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IRegionService;
import com.jzo2o.foundations.service.IRegionServiceService;
import com.jzo2o.foundations.service.IServeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 赵超
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class RegionServiceServiceImpl implements IRegionServiceService {

    private final ServeMapper serveMapper;

    private final IServeService serveService;

    private final IRegionService regionService;

    private final ServeItemMapper serveItemMapper;


    @Override
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO dto) {
        IPage<ServeResDTO> page = new Page<>(dto.getPageNo(), dto.getPageSize());
        Long regionId = dto.getRegionId();
        List<ServeResDTO> list = serveMapper.page(page, regionId);

        return PageResult.of(list, dto.getPageSize().intValue(), page.getPages(), page.getTotal());
    }

    @Override
    public void batchAdd(List<ServeUpsertReqDTO> serveUpsertReqDTOList) {
        if (CollUtil.isEmpty(serveUpsertReqDTOList)) {
            log.info("服务新增更新列表为空");
            return;
        }
        //检查服务是否存在，启用
        for (ServeUpsertReqDTO dto : serveUpsertReqDTOList) {
            ServeItem serveItem = serveItemMapper.selectById(dto.getServeItemId());
            Integer activeStatus = serveItem.getActiveStatus();
            int status = FoundationStatusEnum.ENABLE.getStatus();
            if (ObjectUtil.isNull(serveItem) || activeStatus != status) {
                log.info("服务不存在,或该区域项目没有启动");
                throw new ForbiddenOperationException("服务不存在,或该区域项目没有启动");
            }

            //检测是否重复
            LambdaQueryWrapper<Serve> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Serve::getServeItemId, dto.getServeItemId())
                    .eq(Serve::getRegionId, dto.getRegionId());
            int count = serveService.count(queryWrapper);
            if (count > 0) {
                log.info("服务已存在");
                throw new ForbiddenOperationException("服务已存在");
            }

            //查询城市编码
            String cityCode = regionService.getById(dto.getRegionId()).getCityCode();

            //添加服务
            Serve serve = new Serve();
            BeanUtils.copyProperties(dto, serve);
            serve.setCityCode(cityCode);
            serveService.save(serve);
        }
    }

    @Override
    public void delete(Long id) {
        serveService.removeById(id);
    }

    @Override
    public void updatePrice(Long id, Double price) {
        if (ObjectUtil.isNull(id) || ObjectUtil.isNull(price)) {
            log.info("参数错误");
            throw new ForbiddenOperationException("参数错误");
        }

        Serve serve = serveService.getById(id);
        serve.setPrice(BigDecimal.valueOf(price));
        String priceStr = price + "";
        BigDecimal bdPrice = new BigDecimal(priceStr).setScale(2, RoundingMode.HALF_UP);
        serveService.lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getPrice, bdPrice)
                .update();
    }

    @Override
    public void updateOnHot(Long id) {
        Integer isHot = serveService.getById(id).getIsHot() == 1 ? 0 : 1;
        serveService.lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getIsHot, isHot)
                .update();
    }


    @Override
    public void updateOnSale(Long id) {
        Integer saleStatus = serveService.getById(id).getSaleStatus() == 1 ? 2 : 1;

        serveService.lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getSaleStatus, saleStatus)
                .update();
    }


}
